package com.warom.sdg.webview.jsinterface;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.warom.sdg.dto.ThresholdUpdateDto;
import com.warom.sdg.model.ApiResponse;
import com.warom.sdg.model.ThresholdSettingEntity;
import com.warom.sdg.service.ThresholdSettingService;
import com.warom.sdg.webview.BaseWebViewJsInterface;
import com.warom.sdg.webview.WebViewWrapper;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

/**
 * WebView JavaScript接口 - 阈值设置
 * 处理阈值设置相关的JavaScript调用
 */
public class WebViewJsThresholdSettingInterface extends BaseWebViewJsInterface {
    private static final String TAG = "ThresholdSettingJsInterface";
    private final ThresholdSettingService thresholdSettingService;
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
    private final Gson gson = new Gson();

    public WebViewJsThresholdSettingInterface(Context context) {
        super(context, null);
        this.thresholdSettingService = new ThresholdSettingService((Application) context.getApplicationContext());
    }

    /**
     * 构造函数，带WebViewWrapper参数
     * @param context 上下文
     * @param webViewWrapper WebView包装类，用于回调
     */
    public WebViewJsThresholdSettingInterface(Context context, WebViewWrapper webViewWrapper) {
        super(context, webViewWrapper);
        this.thresholdSettingService = new ThresholdSettingService((Application) context.getApplicationContext());
    }

    /**
     * 异步获取阈值设置列表
     * @param args 空参数，为保持接口一致性
     * @param requestId 请求ID
     */
    @JavascriptInterface
    public void getThresholdSettings(String args, String requestId) {
        executor.execute(() -> getSettingsList(args, result -> handleAsyncResponse(requestId, result)));
    }

    private void getSettingsList(String args, Consumer<String> callback) {
        try {
            ApiResponse<List<ThresholdSettingEntity>> response = thresholdSettingService.getThresholdSettings();
            callback.accept(gson.toJson(response));
        } catch (Exception e) {
            Log.e(TAG, "获取阈值设置列表失败", e);
            ApiResponse<Object> errorResponse = new ApiResponse<>(false, e.getMessage(), null);
            callback.accept(gson.toJson(errorResponse));
        }
    }

    /**
     * 异步更新单条阈值设置
     * @param params JSON字符串格式的ThresholdSettingEntity
     * @param requestId 请求ID
     */
    @JavascriptInterface
    public void updateThresholdSetting(String params, String requestId) {
        executor.execute(() -> updateSetting(params, result -> handleAsyncResponse(requestId, result)));
    }

    private void updateSetting(String params, Consumer<String> callback) {
        try {
            ThresholdUpdateDto updateDto = gson.fromJson(params, ThresholdUpdateDto.class);
            Log.d(TAG, "解析到阈值更新请求: " + updateDto);
            
            ApiResponse<ThresholdSettingEntity> response = thresholdSettingService.updateThresholdSetting(updateDto);
            callback.accept(gson.toJson(response));
        } catch (Exception e) {
            Log.e(TAG, "更新阈值范围失败", e);
            ApiResponse<Object> errorResponse = new ApiResponse<>(false, e.getMessage(), null);
            callback.accept(gson.toJson(errorResponse));
        }
    }
} 